package com.jinchengll.xigua.ui.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.jinchengll.xigua.R;
import com.jinchengll.xigua.constant.ItemType;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jinchengll
 * @since 2021/10/10
 */
public class DiffViewRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final static String LOG_TAG = "DiffViewAdapter";

    private List<ItemType> typeList;
    private Context context;

    public DiffViewRecyclerViewAdapter(List<ItemType> types, Context context) {
        this.typeList = types;
        this.context = context;
    }

    public void setTypeList(List<ItemType> typeList) {
        this.typeList = typeList;
    }

    public class Item1ViewHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public Item1ViewHolder(View view) {
            super(view);
            textView = view.findViewById(R.id.itemTextView);
        }
    }

    public class Item2ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;

        public Item2ViewHolder(View view) {
            super(view);
            imageView = view.findViewById(R.id.itemImageView);
        }
    }

    public class Item3ViewHolder extends RecyclerView.ViewHolder {
        RecyclerView recyclerView;

        public Item3ViewHolder(View view) {
            super(view);
            recyclerView = view.findViewById(R.id.itemRecyclerView);
            // 这是横向滑动
            LinearLayoutManager layoutManager = new LinearLayoutManager(context);
            layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            recyclerView.setLayoutManager(layoutManager);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        if (viewType == ItemType.Text.getValue()) {
            View view = LayoutInflater.from(context).inflate(R.layout.diff_item_1, parent, false);
            viewHolder = new Item1ViewHolder(view);
        } else if (viewType == ItemType.Image.getValue()) {
            View view = LayoutInflater.from(context).inflate(R.layout.diff_item_2, parent, false);
            viewHolder = new Item2ViewHolder(view);
        } else if (viewType == ItemType.RecyclerView.getValue()) {
            View view = LayoutInflater.from(context).inflate(R.layout.diff_item_3, parent, false);
            viewHolder = new Item3ViewHolder(view);
        } else {
            // 应当在这里增加不存在的判断
            Log.e(LOG_TAG, "出现不存在的ItemType");
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof Item1ViewHolder) {
            Item1ViewHolder viewHolder = (Item1ViewHolder) holder;
            // 假设这里直接设置数据
            viewHolder.textView.setText("TextView" + position);
        } else if (holder instanceof Item2ViewHolder) {
            Item2ViewHolder viewHolder = (Item2ViewHolder) holder;
            // 设置图片（只为了填充数据，先直接填写）
            Glide.with(context).load("https://pic.ntimg.cn/20110811/8029346_082444436000_2.jpg")
                    .into(viewHolder.imageView);
        } else if (holder instanceof Item3ViewHolder) {
            Item3ViewHolder viewHolder = (Item3ViewHolder) holder;
            // 设置recyclerView
            List<String> textList = new ArrayList<>();
            for (int i = 0; i < 6; i++) {
                textList.add("横向滑动" + i);
            }
            Item3RecyclerViewAdapter adapter = new Item3RecyclerViewAdapter(textList, context);
            viewHolder.recyclerView.setAdapter(adapter);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return typeList.get(position).getValue();
    }

    @Override
    public int getItemCount() {
        return typeList.size();
    }
}
